package ru.spbu.math.android.cbrf.updating;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import ru.spbu.math.android.cbrf.model.Rate;
import ru.spbu.math.android.cbrf.model.StockItem;
import ru.spbu.math.android.cbrf.view.UsdCourseWidget;

import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * User: yan lobkarev
 * mail: yan.lobkarev@gmail.com
 * Date: 04.05.2010
 * Time: 9:47:48
 *
 * This class are to send Rate to Widget.
 */
public class ForWidgetRatesSender {


    /*
    *      to send updates to Widget.
    */

    public void updateWidgetForLatestRate(Rate r, Context context) {
        Intent updateWidgIntent = new Intent(context, UsdCourseWidget.class);
        updateWidgIntent.setAction(UsdCourseWidget.UPDATE_CONTENT_ACTION);  //  action that allows receive updates
        updateWidgIntent.putExtras(packRateInExtras(r));                //  put the base data
        context.sendBroadcast(updateWidgIntent);                       //  sending...
    }


    /*
     * return your Rate's data & Change in Bundle
     */

    private Bundle packRateInExtras(Rate r) {
        Bundle extras = new Bundle();

        //  take off a simple Rate to demonstrate
        //  todo: redact last four lines.
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM");
        extras.putString("DATE", dateFormat.format(r.getDate()));
        extras.putString("VALUE", String.valueOf(r.getValue()));
        extras.putString("CHANGE", "*0.***");
        extras.putString("SOURCE", r.getSource().getCode());
        extras.putString("TARGET", r.getTarget().getCode());
        extras.putString("NAME", r.getSource().getCode());
        return extras;
    }


}
